/*
 * @author Javier A. Ramírez M.
 * jaramirez85@yahoo.es
 */

package com.jav;

import com.jav.data.InvMovilData;
import com.jav.per.PersisCaptura;
import com.jav.per.Persistencia;
import com.sun.lwuit.Button;
import com.sun.lwuit.Command;
import com.sun.lwuit.Component;
import com.sun.lwuit.Container;
import com.sun.lwuit.Form;
import com.sun.lwuit.Label;
import com.sun.lwuit.TabbedPane;
import com.sun.lwuit.animations.Transition3D;
import com.sun.lwuit.events.ActionEvent;
import com.sun.lwuit.geom.Dimension;
import com.sun.lwuit.layouts.BorderLayout;
import com.sun.lwuit.layouts.BoxLayout;
import com.sun.lwuit.util.Resources;
import javax.microedition.rms.RecordEnumeration;


public class ReportForm extends Form {
    
    private TabbedPane tab = new TabbedPane();    
    private Button bt_valInv = createButton();
    

    public ReportForm() {
        super("INFORMES");
        setLayout(new BorderLayout());
        try {
            setTransitionInAnimator(Transition3D.createSwingIn(500));
            setTransitionOutAnimator(Transition3D.createSwingIn(500, false));
            Resources res = Datos.getInstance().getRes();
            Container ct1 = new Container(new BoxLayout(BoxLayout.Y_AXIS));
            ct1.addComponent(createPair("Total Inv $", bt_valInv, 20));
            

            tab.addTab("TOTAL INVE.", ct1);
            tab.addTab("PRODUCTO", new ProductoContainer());
            addComponent("Center", tab);
            
            addCommand(new Command("Atrás", res.getImage("bck.png")) {

                public void actionPerformed(ActionEvent evt) {
                    InvMovilMidlet.getMainForm().show();
                }
            });
            
            addCommand(new Command("Next", res.getImage("playtrack.png")) {

                public void actionPerformed(ActionEvent evt) {
                    int i = tab.getSelectedIndex();
                    if (i == 1) {
                        i = 0;
                    } else {
                        i++;
                    }
                    tab.setSelectedIndex(i);
                }
            });
            cargaDatosInvetario();
            
        } catch (Exception e) {
            Datos.getInstance().getLogs().append(e.getMessage());
        }
    }
    
    protected Container createPair(String label, Component c, int minWidth) {
        Container pair = new Container(new BorderLayout());
        Label l =  new Label(label);
        Dimension d = l.getPreferredSize();
        d.setWidth(Math.max(d.getWidth(), minWidth));
        l.setPreferredSize(d);
        l.getStyle().setBgTransparency(100);
        pair.addComponent(BorderLayout.WEST,l);
        pair.addComponent(BorderLayout.CENTER, c);
        return pair;
    }

    private void cargaDatosInvetario() {

        bt_valInv.setText("$ "+ Float.toString(getTotalInventario()));
        repaint();

    }

    private Button createButton() {
        Button boton = new Button(""){

            public void startTicker(long delay, boolean rightToLeft) {
                super.startTicker(100, true);
            }

        };
        boton.setAlignment(CENTER);
        return boton;
    }

    private float getTotalInventario(){

        Persistencia p = null;
        float totalinventario = 0;
        try {
            p = new Persistencia(InvMovilData.PERS_CAPTURA);
            RecordEnumeration re = p.getRecordStore().enumerateRecords(null,null,false);
            while (re.hasNextElement()){
                PersisCaptura pd = new PersisCaptura();
                pd.resurrect(re.nextRecord());
                totalinventario += ((pd.getPacas()*pd.getUnidadesXpaca())+pd.getUnidades())*pd.getPrecio();
            }

        }
        catch (Exception ex) {
                ex.printStackTrace();
        }
        finally{
            try {
                if(p != null){
                    p.close();
                }
            } catch (Exception ex) {
                ex.printStackTrace();
            }
        }

        return totalinventario;

    }

 
}
